'use strict';

require('../../assets/scss/entry/common.scss');
require('../../assets/scss/entry/login.scss');

let $ = jQuery = window.jQuery = window.$ = require('jquery');

import * as api from '../../components/api/index.js';
import url from '../../components/util/url.js';
require('../../libs/jquery.cookie');

let pageEntry = (function() {
	// let mediaId = url.getQuery('mediaId');
	/**
	 * 验证手机号
	 * @param tel {Number} 手机号码
	 */
	let verifyTel = (tel) => {
		let telReg = /^(13[0-9]|15[0-9]|18[0-9]|14[0-9]|17[0-9])\d{8}$/;

		// 验证手机号是否为空
		if (!tel) {
			$(".error").hide();
			$(".content-right input").css("borderColor","rgb( 231, 231, 231 )");
			$(".parentTel").empty().html("*手机号不能为空");
			$("#parentTel").css("borderColor","rgb( 255, 141, 50 )");
			$(".parentTel").show();
			return;
		}
		// 验证手机号是否正确
		if (!telReg.test(tel)) {
			$(".error").hide();
			$(".content-right input").css("borderColor","rgb( 231, 231, 231 )");
			$(".parentTel").empty().html("*请输入正确的手机号");
			$("#parentTel").css("borderColor","rgb( 255, 141, 50 )");
			$(".parentTel").show();
			return;
		}

		return true;
	}

	/**
	 * 验证手机验证码
	 * @param checkCode {Number} 手机验证码
	 */
	let verifyCheckCode = (checkCode) => {
		let checkCodeReg = /^\d{4}$/;

		// 验证手机验证码是否为空
		if (!checkCode) {
			$(".error").hide();
			$(".content-right input").css("borderColor","rgb( 231, 231, 231 )");
			$(".checkCode").empty().html("*验证码不能为空");
			$("#checkCode").css("borderColor","rgb( 255, 141, 50 )");
			$(".checkCode").show();
			return;
		}
		// 验证手机验证码是否正确
		if (!checkCodeReg.test(checkCode)) {
			$(".error").hide();
			$(".content-right input").css("borderColor","rgb( 231, 231, 231 )");
			$(".checkCode").empty().html("*请输入正确的验证码");
			$("#checkCode").css("borderColor","rgb( 255, 141, 50 )");
			$(".checkCode").show();
			return;
		}

		return true;
	}
	/**
	 * 验证码倒计时
	 * @param time {Number} 倒计时时间
	 * @param checkCodeBtn {jquery Dom} 验证码按钮
	 * @param checkCodeBtnPlaceholder {jquery Dom} 倒计时按钮
	 */
	let countdown = (time, checkCodeBtn, checkCodeBtnPlaceholder) => {
	    var resend = time;
	    checkCodeBtn.hide();
	    // checkCodeBtnPlaceholder.show();
	    checkCodeBtnPlaceholder.css("display","inline-block");

	    var timer = setInterval(function() {
	        var second =  --resend;
	        checkCodeBtnPlaceholder.html(second + 's');

	        if(resend == 0) {
	            checkCodeBtnPlaceholder.html('60s').hide();
	            checkCodeBtn.show();
	            clearTimeout(timer);
	        }
	    }, 1000);
	}
	return {
		setWindowHeight() {
			// 获取视窗高度，将背景图铺满屏幕
			let wh = (window.innerHeight > 0) ? window.innerHeight : screen.height;
			$('body').height(wh);
		},
		bindEvent() {
			// 绑定登录事件
			$(".footer-content-login").off("click").on("click", function(event) {
				let parentTel = $("#parentTel").val();//手机号
				let checkCode = $("#checkCode").val();//手机验证码
				if(verifyTel(parentTel) && verifyCheckCode(checkCode)){
					api.login(checkCode,parentTel,"1").then(resp=>{
						if(resp.ret == "0"){
							$.cookie("userCode",resp.content.userCode,{expires: 1});
							$.cookie("userType",resp.content.userType,{expires: 1});
							window.location = "/agencyApply.html?agencyCode=" + resp.content.register.orgCode;
						}else{
							if(resp.code == 400){//手机号不存在
								$(".error").hide();
								$(".content-right input").css("borderColor","rgb( 231, 231, 231 )");
								$(".checkCode").empty().html("*"+resp.msg);
								// $("#checkCode").css("borderColor","rgb( 255, 141, 50 )");
								$(".checkCode").show();
							}else if(resp.code == 500){//验证码错误
								$(".error").hide();
								$(".content-right input").css("borderColor","rgb( 231, 231, 231 )");
								$(".checkCode").empty().html("*"+resp.msg);
								// $("#checkCode").css("borderColor","rgb( 255, 141, 50 )");
								$(".checkCode").show();
							}else{//系统异常，请稍后再试
								$(".error").hide();
								$(".content-right input").css("borderColor","rgb( 231, 231, 231 )");
								$(".checkCode").empty().html("*"+resp.msg);
								// $("#checkCode").css("borderColor","rgb( 255, 141, 50 )");
								$(".checkCode").show();
							}
						}
						
					});
				}
			});
			//绑定发送验证码按钮
			$(".content-right .send").off("click").on("click",function(event){
				let parentTel = $("#parentTel").val();//手机号
				if(verifyTel(parentTel)){
					$(".error").hide();
					$(".content-right input").css("borderColor","rgb( 231, 231, 231 )");
					countdown(60,$(".send"),$(".time"));
					api.sendSmsCode("",parentTel).then(resp=>{
						// if(resp.ret == "0"){//发送成功
							// countdown(60,$(".send"),$(".time"));
						// }else if(resp.ret == 300){//手机号不存在
						// 	$(".error").hide();
						// 	$(".content-right input").css("borderColor","rgb( 231, 231, 231 )");
						// 	$(".parentTel").empty().html("*"+resp.msg);
						// 	$("#parentTel").css("borderColor","rgb( 255, 141, 50 )");
						// 	$(".parentTel").show();
						// }
					});
				}
			});
			//绑定跳转注册页面
			$(".footer-content-bottom").off("click").on("click",function(event){
				window.location = "/agencyRegister.html";
			});
		}
	}
})();

pageEntry.setWindowHeight();
pageEntry.bindEvent();
